﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace SecureCommander.Core.UserManagement
{
    public class UserManager
    {
        public static UserInfo Import(string pathfile)
        {
            UserInfo user = null;
            try
            {
                XmlDocument xmldoc = new XmlDocument();
                user = new UserInfo();
                xmldoc.Load(pathfile);

                XmlElement User = xmldoc.DocumentElement;

                XmlElement Name = (XmlElement)User.ChildNodes[0];
                user.Name = Name.InnerText;

                XmlElement DateofBirth = (XmlElement)User.ChildNodes[1];
                user.DateOfbirth = DateTime.Parse(DateofBirth.InnerText);

                XmlElement Address = (XmlElement)User.ChildNodes[2];
                user.Address = Address.InnerText;

                XmlElement Phone = (XmlElement)User.ChildNodes[3];
                user.Phone = Phone.InnerText;

                XmlElement Mail = (XmlElement)User.ChildNodes[4];
                user.Mail = Mail.InnerText;

                XmlElement Pubkey = (XmlElement)User.ChildNodes[5];
                user.Publickey = Pubkey.InnerText;

                XmlElement Prikey = (XmlElement)User.ChildNodes[6];
                user.Privatekey = Prikey.InnerText;

                XmlElement Hash = (XmlElement)User.ChildNodes[7];
                user.Hash = Hash.InnerText;

                XmlElement Salt = (XmlElement)User.ChildNodes[8];
                user.Salt = Salt.InnerText;
            }
            catch { }
            return user;
        }

        public static bool Export(UserInfo user, string pathfile)
        {
            try
            {
                XmlDocument xmldoc = new XmlDocument();

                XmlElement UserNode = xmldoc.CreateElement("User");
                xmldoc.AppendChild(UserNode);

                XmlElement NameNode = xmldoc.CreateElement("Name");
                NameNode.InnerText = user.Name;
                UserNode.AppendChild(NameNode);

                XmlElement DateofBirthNode = xmldoc.CreateElement("DateOfBirth");
                DateofBirthNode.InnerText = user.DateOfbirth.ToShortDateString();
                UserNode.AppendChild(DateofBirthNode);

                XmlElement AddressNode = xmldoc.CreateElement("Address");
                AddressNode.InnerText = user.Address;
                UserNode.AppendChild(AddressNode);

                XmlElement PhoneNode = xmldoc.CreateElement("Phone");
                PhoneNode.InnerText = user.Phone;
                UserNode.AppendChild(PhoneNode);

                XmlElement MailNode = xmldoc.CreateElement("Mail");
                MailNode.InnerText = user.Mail;
                UserNode.AppendChild(MailNode);

                XmlElement PubKeyNode = xmldoc.CreateElement("PublicKey");
                PubKeyNode.InnerText = user.Publickey;
                UserNode.AppendChild(PubKeyNode);

                XmlElement PriNode = xmldoc.CreateElement("PrivateKey");
                PriNode.InnerText = user.Privatekey;
                UserNode.AppendChild(PriNode);

                XmlElement HashNode = xmldoc.CreateElement("Hash");
                HashNode.InnerText = user.Hash;
                UserNode.AppendChild(HashNode);

                XmlElement SaltNode = xmldoc.CreateElement("Salt");
                SaltNode.InnerText = user.Salt;
                UserNode.AppendChild(SaltNode);

                xmldoc.Save(pathfile);
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}
